package jlib.component.tool {
	import jlib.utils.GraphicsUtil;
	
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;

	/**
	 * @author 박종희
	 */
	public final class DragSelectBox extends Shape implements IMouseInteractive {
		
		private var color:uint = 0x666666;
		private var option:String;
		private var startPoint:Point;
		private var endPoint:Point;
		private var target:DisplayObject;
		
		public function DragSelectBox( graphic:Graphics = null, option:String="dashed" ): void
		{
			this.option = option, startPoint = new Point(), endPoint = new Point();
		}
		
		public function press( e:MouseEvent ): void
		{
			var matrix:Matrix = stage.transform.matrix;
			var invert:Matrix = this.parent.transform.matrix;
			invert.invert();
			matrix.concat(invert);
			this.transform.matrix = matrix;
			startPoint = new Point(this.mouseX, this.mouseY); //target.globalToLocal(new Point(target.stage.mouseX, target.stage.mouseY));
		}
		
		public function move( e:MouseEvent ): void
		{
			this.graphics.clear();
			this.graphics.lineStyle(0,this.color,1);
			
			var endPoint:Point = new Point(this.mouseX, this.mouseY);
			
			if(option == "dashed"){
				GraphicsUtil.drawDashLine(this.graphics, startPoint.x, startPoint.y, endPoint.x, startPoint.y , 4, 2);
				GraphicsUtil.drawDashLine(this.graphics, endPoint.x, startPoint.y, endPoint.x, endPoint.y , 4, 2);
				GraphicsUtil.drawDashLine(this.graphics, endPoint.x, endPoint.y, startPoint.x, endPoint.y, 4, 2);
				GraphicsUtil.drawDashLine(this.graphics, startPoint.x, endPoint.y, startPoint.x,  startPoint.y, 4, 2);
			}else{
				this.graphics.drawRect( startPoint.x , startPoint.y , this.mouseX-startPoint.x , this.mouseY-startPoint.y );
			}
		}
		
		public function release( e:MouseEvent ): void
		{
			this.graphics.clear();
			endPoint.x = this.mouseX-startPoint.x, endPoint.y = this.mouseY-startPoint.y;
			//Mouse.show();
		}
		
		public function setColor( color:uint ): void { this.color = color; }
		
		public function getBoundary(): Rectangle { return new Rectangle(startPoint.x, startPoint.y, endPoint.x, endPoint.y); }
		
		public function setTarget( value:DisplayObject ): void
		{
			target = value;
		}
		
	}//c
}//p
